package br.com.danieljr.crowdmap.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import br.com.danieljr.crowdmap.basededados.BaseDadosArquivo;
import br.com.danieljr.crowdmap.basededados.Dados;
import br.com.danieljr.crowdmap.basededados.IControladorBaseDados;
import br.com.danieljr.crowdmap.modelos.Bateria;
import br.com.danieljr.crowdmap.modelos.Operadora;

public class ColetaDadosService extends IntentService implements
		LocationListener {

	IControladorBaseDados bd;
	double latitude, longitude;
	int sinal;
	LocationManager locationManager;
	private TelephonyManager mTelephonyManager;
	private PhoneStateListener mPhoneStateListener;
	Bateria bateria;
	Operadora operadora;

	public ColetaDadosService() {
		super("Servico de salvar dados");
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub mTelephonyManager =
		// (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
		mTelephonyManager = (TelephonyManager) this
				.getSystemService(TELEPHONY_SERVICE);
		mPhoneStateListener = new PhoneStateListener() {
			@Override
			public void onSignalStrengthsChanged(SignalStrength signalStrength) {
				sinal = signalStrength.getGsmSignalStrength();
				super.onSignalStrengthsChanged(signalStrength);
			}
		};
		bateria = new Bateria(this);
		operadora = new Operadora(this);
		bd = new BaseDadosArquivo("dados.txt");
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.d("SalvarService", "Service iniciado");
		mTelephonyManager.listen(mPhoneStateListener,
				PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		bateria.atualizaBateria();
		Dados dados = recuperarDados();
		dados.setSinal(sinal);
		if (dados.isCompletos()) {
			bd.abrir();
			bd.inserir(dados);
			bd.fechar();
			stopSelf();
		}
		return START_FLAG_REDELIVERY;
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		stopSelf();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.d("Service", "Encerrando o Service");
		mTelephonyManager.listen(mPhoneStateListener,
				PhoneStateListener.LISTEN_NONE);
		locationManager.removeUpdates(this);
		super.onDestroy();
	}

	private Dados recuperarDados() {
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			locationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, 0, 0, this);
		} else {
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, 0, 0, this);
		}
		Dados dados = new Dados();
		dados.setLatitude(latitude);
		dados.setLongitude(longitude);
		dados.setOperadora(operadora.getNome());
		dados.setBateria(bateria.getNivel());
		return dados;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		latitude = location.getLatitude();
		longitude = location.getLongitude();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
